// 发送消息到AI服务
  const sendMessage = async (content: string): Promise<{ content: string }> => {
    if (!currentSessionId.value) {
      createSession()
    }

    // 添加用户消息
    addUserMessage(content)

    // 设置加载状态
    setLoading(true)
    setError(null)

    try {
      // 调用真实的AI API
      const response = await request.post<{ content: string }>(API_ENDPOINTS.ZHIPU_CHAT, {
        message: content
      })

      // 添加助手消息
      addAssistantMessage(response.content)

      // 保存到本地存储
      saveToLocalStorage()

      return response
    } catch (err) {
      const errorMessage = err instanceof Error ? err.message : '发送消息失败'
      setError(errorMessage)
      throw new Error(errorMessage)
    } finally {
      setLoading(false)
    }
  }

  // 发送流式消息到AI服务
  const sendStreamMessage = async function* (content: string): AsyncGenerator<{ content: string }> {
    if (!currentSessionId.value) {
      createSession()
    }

    // 添加用户消息
    addUserMessage(content)

    // 设置加载状态
    setLoading(true)
    setError(null)

    try {
      // 调用真实的流式AI API
      const response = await fetch(`${API_ENDPOINTS.ZHIPU_STREAM}`, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({ message: content })
      })

      if (!response.ok) {
        const errorText = await response.text()
        throw new Error(`HTTP error! status: ${response.status}, message: ${errorText}`)
      }

      const reader = response.body?.getReader()
      const decoder = new TextDecoder()

      if (!reader) {
        throw new Error('无法获取响应流')
      }

      let buffer = ''
      let assistantMessageAdded = false

      while (true) {
        const { done, value } = await reader.read()
        if (done) break

        buffer += decoder.decode(value, { stream: true })
        const lines = buffer.split('\n')
        buffer = lines.pop() || '' // 保留最后一个不完整的行

        for (const line of lines) {
          if (line.startsWith('data: ')) {
            const data = line.slice(6)
            if (data === '[DONE]') {
              return
            }

            try {
              const chunk = JSON.parse(data)
              const content = chunk.choices?.[0]?.delta?.content || ''

              if (content && !assistantMessageAdded) {
                // 添加助手消息
                addAssistantMessage('')
                assistantMessageAdded = true
              }

              if (content) {
                // 更新最后一条助手消息的内容
                const lastMessage = currentMessages.value[currentMessages.value.length - 1]
                if (lastMessage && lastMessage.role === 'assistant') {
                  lastMessage.content += content
                  currentSession.value!.updatedAt = Date.now()
                }

                yield { content }
              }
            } catch (e) {
              console.error('解析流数据失败:', e, '原始数据:', data)
            }
          }
        }
      }

      // 保存到本地存储
      saveToLocalStorage()

    } catch (err) {
      const errorMessage = err instanceof Error ? err.message : '发送消息失败'
      setError(errorMessage)
      throw new Error(errorMessage)
    } finally {
      setLoading(false)
    }
  }